package com.httpclasses;
import java.io.IOException;
import java.io.StringReader;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XMLReaderLunch {

	public static List<RSSFeed> lol(int dir,String cont) {

		List<RSSFeed> items = new LinkedList<RSSFeed>();

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

		// Using factory get an instance of document builder
		DocumentBuilder db;
		try {
			db = dbf.newDocumentBuilder();

			StringReader reader = new StringReader(cont);
			InputSource inputSource = new InputSource(reader);

			// parse using builder to get DOM representation of the XML file
			Document dom;
			// System.out.println( ConnectionTest.inputStreamToString(
			// (ConnectionTest.getData() )));
			// dom = db.parse("C:\\Users\\berka\\Desktop\\vasttrafikEx.xml");
			// dom = db.parse(ConnectionTest.getData());
			dom = db.parse(inputSource);
			reader.close();
			// dom = db.parse(new
			// URL("http://www.vasttrafik.se/External_Services/NextTrip.asmx/GetForecast?identifier=spaldingsgatan&stopId=00006160").openStream());
			// dom =
			// db.parse("http://www.vasttrafik.se/External_Services/NextTrip.asmx/GetForecast?identifier=spaldingsgatan&stopId=00006160");

			// dom = db.pars
			Element docEle = dom.getDocumentElement();

			// get a nodelist of elements
			NodeList nl = docEle.getElementsByTagName("item");
			if (nl != null && nl.getLength() > 0) {
				for (int i = 0; i < nl.getLength(); i++) {

					// get the employee element
					Element el = (Element) nl.item(i);

					// get the Employee object
					RSSFeed e = getLunchItem(el);

					// add it to list
					items.add(e);
				}
			}

			return items;

		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;

	}

	/**
	 * I take a xml element and the tag name, look for the tag and get the text
	 * content i.e for <employee><name>John</name></employee> xml snippet if the
	 * Element points to employee node and tagName is 'name' I will return John
	 */
	private static String getTextValue(Element ele, String tagName) {
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		if (nl != null && nl.getLength() > 0) {
			Element el = (Element) nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}

		return textVal;
	}

	/**
	 * Calls getTextValue and returns a int value
	 */
	private static int getIntValue(Element ele, String tagName) {
		// in production application you would catch the exception
		return Integer.parseInt(getTextValue(ele, tagName));
	}

	private static RSSFeed getLunchItem(Element obj) {
		String title = getTextValue(obj,"title");
		String description = getTextValue(obj,"description");
		RSSFeed y = new RSSFeed(title,description);
		return y;

	}
}